<?php

namespace App\Models\Factory;

use App\Helpers\DateUtils;
use App\Helpers\Generator\TokenGenerator;
use App\Helpers\RestResponseFactory;
use App\Helpers\RestUtils;
use App\Helpers\Utils;
use App\Models\AbsModelFactory;
use App\Models\Orm\Sign;
use App\Models\Orm\User;
use App\Models\Orm\UserBanks;
use App\Models\Orm\UserCertify;
use App\Models\Orm\UserIdentity;
use App\Models\Orm\UserInvite;
use App\Models\Orm\UserInviteCode;
use App\Models\Orm\UserProfile;
use App\Strategies\SexStrategy;

/**
 * 用户工厂
 * Class UserFactory
 * @package App\Models\Factory
 */
class UserFactory extends AbsModelFactory
{


    /**
     * 通过手机号查询用户信息
     */
    public static function getUserByMobile($mobile)
    {
        $user = User::where(['phone' => $mobile])->first();
        return $user?$user : '';
    }

    /**
     * 通过token查找用户对象
     *
     * @param $token
     * @return mixed
     */
    public static function getUserByToken($token)
    {
        return User::where('token', $token)->first();
    }


    //更新用户token和最后登录时间
    public static function updateUser($mobile,$token)
    {
        $data['token'] = $token;
        $data['lass_time'] = DateUtils::getToday();
        $data['ip'] = Utils::ipAddress();
        $user = User::where(['phone' => $mobile])->update($data);
        return $user?true : false;
    }


    //发短信插入数据库
    public static function addUser($mobile,$token)
    {
        $user = User::firstOrCreate(
            [
                'phone' => $mobile
            ],
            [
                'phone' => $mobile,
                'registe_time' => DateUtils::getToday(),
                'created_ip' => Utils::ipAddress(),
            ]
        );
    }

    /*
     * 修改登录时间
     *
     */
    public static function addUserLassTime($mobile)
    {
        $time['lass_time'] = DateUtils::getToday();
        $user = User::where(['phone' => $mobile])->update($time);
        return $user;
    }

    /*
     * 获取最后登录时间
     *
     */
    public static function getUserLassTime($mobile)
    {

        $lass_time = User::select('lass_time')->where(['userphone' => $mobile])->first();
        return $lass_time->lass_time;
    }



    /**
     * 创建
     * @param $mobile
     */
    public static function createUser($mobile,$token,$terminal)
    {

        $logo = config("anwen.logo_url").'logo/logo.png';
        $user = User::create(
               [ 'token' => $token,
                'phone' => $mobile,
                'registe_time' => DateUtils::getToday(),
                'head_image' => $logo,
                'nick_name' => "Pembukuan".rand(10000000, 99999999),
                'number' => date('Ymd').rand(100000, 999999),
                'terminal' => $terminal,
                'ip' => Utils::ipAddress()
            ]

        );
        return $user? true : false;
    }

    /*
     *
     */

    public static  function getIdCard($mobile)
    {
        $user = User::where('phone',$mobile)->first();
        return $user->id_card ? 1 : 0;
    }

    /**
     * 生成的token保存进用户表
     */
    public static function addUserTokenByMobile($mobile,$token)
    {
        $user = User::where(['phone' => $mobile])->update($token);
        return $user;
    }


    /*
     * 通过token获取到用户id
     */

    public static function getIdByToken($token)
    {
        $user = User::where(['token' => $token])->first();
        return $user ? $user->id : '';
    }

    //添加用户身份证号
    public static function addIdCard($user_id,$data)
    {
        $user['id_card'] = $data['id_card'];
        $user['real_name'] = $data['real_name'];
        $result = User::where('id',$user_id)->update($user);
        return $result ? true : false;
    }

    //通过token获取用户信息
    public static  function getUser($token)
    {
        $user = User::where(['token' => $token])->first();
        return $user ? $user : "";
    }

    //设置密码
    public static function setPassword($user_id,$data)
    {
        $result = User::where('id',$user_id)->update('password',$data);

        return $result ? true : false;
    }

    //通过用户id获取用户签到信息
    public static function getSign($user_id)
    {

        $result = Sign::where('user_id',$user_id)->first();

        return $result ? $result : '';
    }

    //通过用户id获取用户签到信息
    public static function getUserById($user_id)
    {

        $result = User::where('id',$user_id)->first();

        return $result ? $result : '';
    }


}